#include "StdAfx.h"
#include "TextUtility.h"

int CTextUtility::UTF8ToGB(const CHAR* utf8, LPWSTR lpSrc, int nBuffSize)
{
    int nRet = 0;
    int i = MultiByteToWideChar(CP_UTF8, 0, utf8, -1, NULL, 0);
    if (i < nBuffSize)
    {
        nRet = i;
        MultiByteToWideChar(CP_UTF8, 0, utf8, -1, lpSrc, i);
    }
    return nRet;
}

int CTextUtility::MultiToWChars(LPCSTR lpSrc, LPWSTR lpDes, int nBuffSize)
{
    if (NULL == lpSrc)
    {
        return 0;
    }

    int nRet = 0;
    int i = MultiByteToWideChar(CP_ACP, 0, lpSrc, -1, NULL, 0);
    if (i < nBuffSize)
    {
        nRet = i;
        MultiByteToWideChar(CP_ACP, 0, lpSrc, -1, lpDes, i);
    }
    return nRet;
}


bool CTextUtility::UnicodeToMB(const wchar_t* lpwcSrc, LPSTR lpMultiByte, int nBuffSize)
{  
    // convert an widechar string to Multibyte   
    int32 MBLen = WideCharToMultiByte(CP_ACP, 0, lpwcSrc, wcslen(lpwcSrc), NULL, -1, NULL, NULL);  
    if (MBLen >= nBuffSize)  
    {  
        return false;  
    }  

    int nRtn = WideCharToMultiByte(CP_ACP, 0, lpwcSrc, wcslen(lpwcSrc), lpMultiByte, nBuffSize, NULL, NULL);  

    if(nRtn != MBLen)  
    {   
        return false;  
    }  
    return true;  
} 